

package com.kimo.shop.delivery.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kimo.shop.bean.app.dto.MyOrderDto;
import com.kimo.shop.bean.app.dto.MyOrderItemDto;
import com.kimo.shop.bean.app.dto.OrderSelfStationDto;
import com.kimo.shop.bean.model.Order;
import com.kimo.shop.delivery.comment.model.OrderSelfStation;
import com.kimo.shop.delivery.comment.service.OrderSelfStationService;
import com.kimo.shop.security.api.util.SecurityUtils;
import com.kimo.shop.service.MyOrderService;
import com.kimo.shop.service.OrderService;
import com.kimo.shop.service.StationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/p/myStationOrder")
@Api(tags = "用户自提订单接口")
@AllArgsConstructor
public class   MyStationOrderController {

    private final OrderService orderService;

    private final MyOrderService myOrderService;

    private final MapperFacade mapperFacade;

    private final StationService stationService;

    private final OrderSelfStationService orderSelfStationService;

    /**
     * 订单详情自提信息接口
     */
    @GetMapping("/stationDetail")
    @ApiOperation(value = "订单详情自提信息", notes = "根据订单号获取订单详情自提信息")
    @ApiImplicitParam(name = "orderNumber", value = "订单号", required = true, dataType = "String")
    public ResponseEntity<OrderSelfStationDto> orderDetail(@RequestParam(value = "orderNumber", required = true) String orderNumber) {
        OrderSelfStationDto orderSelfStationDto  = orderSelfStationService.getStationByOrderNumber(orderNumber);
        return ResponseEntity.ok(orderSelfStationDto);
    }
    /**
     * 用户扫自提点码获取自提订单列表
     */
    @GetMapping("/getOrderBystationId")
    @ApiOperation(value = "用户扫自提点码", notes = "用户扫自提点码获取自提订单列表")
    @ApiImplicitParam(name = "stationId", value = "订单号", required = true, dataType = "Long")
    public ResponseEntity<List<MyOrderDto>> getOrderListByStationId(@RequestParam(value = "stationId") Long stationId) {
        String userId = SecurityUtils.getUser().getUserId();
        List<MyOrderDto> orderList = myOrderService.getOrderListByStationId(stationId, userId);
        return ResponseEntity.ok(orderList);
    }

    /**
     * 用户提货操作
     * @param orderNumbers
     * @return
     */
    @PutMapping("/orderStationByOrderNumber")
    @ApiOperation(value = "用户提货操作", notes = "用户提货操作")
    @ApiImplicitParam(name = "orderNumbers", value = "订单编号数组", required = true, dataType = "String")
    public ResponseEntity<Boolean> orderStationByOrderNumber(@RequestBody String orderNumbers) {
        List<String> orderNumberList = Arrays.asList(orderNumbers.split(","));
        return ResponseEntity.ok(orderService.orderStationByOrderNumber(orderNumberList, SecurityUtils.getUser().getUserId(),null));
    }

    /**
     * 用户自提信息接口
     */
    @GetMapping("/getStationInfoByOrderNumber")
    @ApiOperation(value = "我的订单自提信息接口", notes = "根据订单号获取订单详情自提信息")
    @ApiImplicitParam(name = "orderNumber", value = "订单号", required = true, dataType = "String")
    public ResponseEntity<MyOrderDto> getStationDetailByOrderNumber(@RequestParam(value = "orderNumber", required = true) String orderNumber) {
        OrderSelfStation orderSelfStation = orderSelfStationService.getOne(new LambdaQueryWrapper<OrderSelfStation>()
                .eq(OrderSelfStation::getOrderNumber,orderNumber));
        OrderSelfStationDto orderSelfStationDto = mapperFacade.map(orderSelfStation, OrderSelfStationDto.class);
        Order order = orderService.getOrderAndOrderItemByOrderNumber(orderNumber);
        //获取订单项的退款信息
        orderService.getOrderItenRefundStatus(order);
        MyOrderDto myOrderDto = mapperFacade.map(order, MyOrderDto.class);
        myOrderDto.setOrderItemDtos(mapperFacade.mapAsList(order.getOrderItems(), MyOrderItemDto.class));
        myOrderDto.setOrderSelfStationDto(orderSelfStationDto);
        return ResponseEntity.ok(myOrderDto);
    }


}
